<template>
  <div class="app-container">
<!--查询条件开始-->
    <el-card class="box-card" style="margin-bottom: 10px;" >
      <div slot="header" class="clearfix">
        <span>查询条件</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="楼栋编号" prop="id">
          <el-input
            v-model="queryParams.floorId"
            placeholder="请输入楼栋编号"
            clearable
            size="small"
            style="width:300px"
          >
            <template slot="append">
              <el-button type="success" plain @click="getFloor">选择楼栋</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="单元编号" prop="name">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入单元编号"
            clearable
            size="small"
            style="width:150px"
          />
        </el-form-item>

        <el-form-item label="有无电梯" prop="name">
          <el-select
            v-model="queryParams.lift"
            placeholder="活动区域"
            clearable
            size="small"
            style="width:150px"
          >
            <el-option label="有" :value="1010"></el-option>
            <el-option label="无" :value="2020"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

<!--表头按钮开始-->
    <el-card class="box-card" style="margin-bottom: 10px;" >
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="openAddUnitPop">新增单元</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
<!--表头按钮结束-->


<!--数据表格开始-->
    <el-table :data="unitList" :header-cell-style="{ 'color': '#3D4144' }">
      <el-table-column label="楼栋编号"  align="center" prop="floorId" />
      <el-table-column label="单元编号"  align="center" prop="id" />
      <el-table-column label="单元别名"  align="center" prop="unitName" />
      <el-table-column label="是否有电梯" align="center" prop="remark" >
        <template slot-scope="scope">
          <span>
            {{ scope.row.lift === '2020'? '无':'有'}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="总层数"  align="center" prop="layerCount"/>
      <el-table-column label="单元面积"  align="center" prop="unitArea">
        <template slot-scope="scope">
          <span> {{ scope.row.unitArea}} </span>㎡
        </template>
      </el-table-column>
      <el-table-column label="单元描述"  align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="openUpdateUnitPop(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="delUnit(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--数据表格结束-->

<!--分页控件开始-->
    <el-pagination
      v-show="total>0"
      :current-page="queryParams.pageNum"
      :page-sizes="[5,10,20,30]"
      :page-size="queryParams.pageSize"
      layout="total,sizes,prev,pager,next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-card>
<!--分页控件结束-->

<!--    修改弹窗-->
    <el-dialog title="修改单元信息" :visible.sync="updateUnitPop" width="25%">
      <UpdateUnit :UnitData="UnitData"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateUnitPop = false">取 消</el-button>
        <el-button type="primary" @click="updateUnit">确 定</el-button>
      </span>
    </el-dialog>

<!--    添加弹窗-->
    <el-dialog title="添加单元" :visible.sync="addUnitPop" width="25%">
      <AddUnit :addUnitData="addUnitData"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addUnitPop = false">取 消</el-button>
        <el-button type="primary" @click="addUnit">确 定</el-button>
      </span>
    </el-dialog>

<!--    选择楼栋窗体-->
    <el-dialog title="选择楼栋" :visible.sync="selectFloorPop" width="30%">
      <el-row>
        <el-col :span="24" :xs="24">
          <el-form :model="floorQuery" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="楼栋名称" prop="name">
              <el-input v-model="floorQuery.name" placeholder="请输入楼栋名称" size="small" style="width: 100%;"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" :xs="24">
          <el-table :data="floorList" highlight-current-row>
            <el-table-column prop="id" label="楼栋ID" width="100px"></el-table-column>
            <el-table-column prop="name" label="楼名称" width="150px"></el-table-column>
            <el-table-column prop="remark" label="备注"></el-table-column>
            <el-table-column label="操作" width="100px" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="xuanze(scope.row.id)">选择</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--      分页开始-->
      <el-pagination
        small
        style="float: right"
        :current-page="floorQuery.pageNum"
        :page-size="floorQuery.pageSize"
        layout="prev, pager, next"
        :total="total"
        :page-sizes="[5]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
      <!--      分页结束-->
    </el-dialog>
    <!--    弹出层结束-->
  </div>
</template>

<script>
import {queryUnit,updateUnit,addUnits,removeUnit} from '@/api/assets/unitApi'
import {queryFloor} from "@/api/village/floor";

import UpdateUnit from './page/updateUnit'
import AddUnit from './page/addUnit'
export default {
  name: "index",
  components:{UpdateUnit,AddUnit},
  data(){
    return {
      // 单元集合
      unitList:[],
      // 楼栋集合
      floorList:[],
      // 查询条件
      queryParams:{
        floorId:undefined,
        id:undefined,
        lift:undefined,
        pageSize:10,
        pageNum:1
      },
      // 总数
      total:0,
      // 修改弹窗
      updateUnitPop: false,
      // 新增弹窗
      addUnitPop :false,
      // 选择楼栋窗体
      selectFloorPop: false,
      // 修改对象
      UnitData: {},
      // 新增对象
      addUnitData:{
        floorId:undefined,
        unitName:undefined,
        layerCount:undefined,
        lift:undefined,
        remark:undefined,
        unitArea:0.00
      },
      // 楼栋模糊查询条件
      floorQuery:{
        pageNum:1,
        pageSize:10,
        name:undefined
      }
    }
  },
  methods:{

    /**
     * 删除单元
     */
    delUnit(id){
      this.$confirm('是否删除楼栋信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeUnit(id).then(res => {
          if(res.data){
            this.handleQuery()
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else{
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /**
     * 新增单元
     */
    addUnit(){
      addUnits(this.addUnitData).then(res => {
        if(res.data){
          this.addUnitPop = false
          this.handleQuery()
          this.$message({
            message: '添加成功',
            type: 'success'
          });
        }
      })
    },

    /**
     * 选择楼栋按钮
     */
    xuanze(id){
      this.selectFloorPop = false
      this.queryParams.floorId = id //模糊查询
      this.addUnitData.floorId = id //选中楼栋
    },

    /**
     * 打开选择楼栋窗体
     */
    getFloor(){
      this.floorQuery.name = undefined
      this.selectFloorPop = true
      const query = {
        pageNum:this.floorQuery.pageNum,
        pageSize:this.floorQuery.pageSize,
        name:this.floorQuery.name
      }
      queryFloor(query).then(res =>{
        this.floorList = res.data.records
      })
    },

    /**
     * 打开新增单元窗体
     */
    openAddUnitPop(){
      if(this.queryParams.floorId === "" || this.queryParams.floorId === undefined){
        this.$message({
          message: '请选择楼栋',
          type: 'warning'
        });
        return false
      }
      this.addUnitPop = true
    },

    /**
     * 修改单元
     */
    updateUnit(){
      updateUnit(this.UnitData).then(res => {
        if(res.data){
          this.updateUnitPop = false
          this.$message({
            message: '修改成功',
            type: 'success'
          });
        }
      })
    },

    /**
     * 打开修改窗体
     */
    openUpdateUnitPop(unit){
      this.UnitData = unit
      this.updateUnitPop = true;
    },

    /**
     * 条件查询
     */
    handleQuery(){
      if(this.queryParams.floorId === "" || this.queryParams.floorId === undefined){
        this.unitList = []
        this.updateUnitPop = false
        this.$message({
          message: '请选择楼栋',
          type: 'warning'
        });
        return false
      }
      queryUnit(this.queryParams).then(res => {
        this.unitList = res.data.records
        this.total = res.data.total
      })
    },

    /**
     *页面大小发生改变
     */
    handleSizeChange(val){
      this.queryParams.pageSize = val
      this.handleQuery()
    },

    /**
     * 当前页数发生改变
     * @param val
     */
    handleCurrentChange(val){
      this.queryParams.pageNum = val
      this.handleQuery()
    },
  },
  watch:{
    'floorQuery.name':{
      handler(newVal,oldVal){
        this.floorQuery.pageNum = 1
        this.getFloor()
      },
      deep:true
    },
    'queryParams.floorId':{
      handler(newVal,oldVal){
        this.floorQuery.pageNum = 1
        this.handleQuery()
      },
      deep:true
    }
  }
}
</script>

<style scoped>

</style>
